package cn.ydxiaoshuai.tools.controller;

import cn.ydxiaoshuai.tools.base.ApiRestController;
import cn.ydxiaoshuai.tools.conts.JobConts;
import cn.ydxiaoshuai.tools.util.MergeImageUtil;
import cn.ydxiaoshuai.tools.util.RedisUtil;
import cn.ydxiaoshuai.tools.vo.BaldGeneralBean;
import cn.ydxiaoshuai.tools.vo.BaldResponseBean;
import cn.ydxiaoshuai.tools.vo.ParamBean;
import cn.ydxiaoshuai.tools.vo.PosterByBase64Bean;
import com.baidu.aip.util.Base64Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * @author 小帅丶
 * @className BaldRestController
 * @Description 海报生成
 * @Date 2020年10月15日22:37:33
 **/
@Controller
@RequestMapping(value = "/rest/poster")
@Scope("prototype")
@Slf4j
public class PosterRestController extends ApiRestController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private MergeImageUtil mergeImageUtil;
    /**
     * @Author 小帅丶
     * @Description 秃头海报生成
     * @Date  2020年10月21日
     * @return void
     **/
    @RequestMapping(value = "/v2/generate",method = {RequestMethod.POST})
    public ResponseEntity<Object> posterGenerateByBase64(PosterByBase64Bean poster){
        log.info("=======访问的方法"+requestURI+"=======访问的IP"+ip+"======访问的User-Agent:"+userAgent);
        BaldGeneralBean bean = new BaldGeneralBean();
        ParamBean paramBean = new ParamBean();
        try {
            startTime = System.currentTimeMillis();
            paramBean = new ParamBean(null,null,poster.getNickName(),poster.getAvatarUrl(),ip,userAgent);
            byte[] imgByte = Base64Util.decode(poster.getImage());
            String posterImg = mergeImageUtil.mergePendant(poster.getNickName(),poster.getJobNum(),imgByte);
            BaldResponseBean.ResultsBean data =  new BaldResponseBean.ResultsBean();
            data.setImage(posterImg);
            bean.success("ok","分享海报",data);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("海报接口出错了"+e.getMessage());
            bean.error("error","当前体验人数较多，请稍等~");
        }
        long endTime = System.currentTimeMillis();
        //耗时
        timeConsuming =String.valueOf((endTime - startTime));
        log.info("海报生成耗时{},接口返回内容",timeConsuming);
        //保存一下请求参数到redis
        redisUtil.lSet(JobConts.PARAM_KEY, paramBean);
        //保存一下结果到redis
        redisUtil.lSet(JobConts.RESULT_KEY, bean);
        //响应的内容
        return new ResponseEntity<Object>(bean, httpHeaders, HttpStatus.OK);
    }

    /**
     * @Author 小帅丶
     * @Description 秃头
     * @Date  2020年10月14日
     * @param file 图片
     * @return void
     **/
    @RequestMapping(value = "/v1/generate",method = {RequestMethod.POST})
    public ResponseEntity<Object> posterGenerate(@RequestParam(value = "file") MultipartFile file){
        log.info("=======访问的方法"+requestURI+"=======访问的IP"+ip+"======访问的User-Agent:"+userAgent);
        //用户微信昵称
        String nickName = ServletRequestUtils.getStringParameter(request, "nickName","");
        //用户微信头像地址
        String avatarUrl = ServletRequestUtils.getStringParameter(request, "avatarUrl","");
        //jobNum
        String jobNum = ServletRequestUtils.getStringParameter(request, "jobNum","");
        BaldGeneralBean bean = new BaldGeneralBean();
        ParamBean paramBean = new ParamBean();
        try {
            startTime = System.currentTimeMillis();
            paramBean = new ParamBean(null,null,nickName,avatarUrl,ip,userAgent);
            String posterImg = mergeImageUtil.mergePendant(nickName,jobNum,file.getBytes());
            BaldResponseBean.ResultsBean data =  new BaldResponseBean.ResultsBean();
            data.setImage(posterImg);
            bean.success("ok","分享海报",data);
        } catch (Exception e) {
            log.info("海报接口出错了"+e.getMessage());
            bean.error("error","当前体验人数较多，请稍等~");
        }
        long endTime = System.currentTimeMillis();
        //耗时
        timeConsuming =String.valueOf((endTime - startTime));
        log.info("耗时{},接口返回内容",timeConsuming);
        //保存一下请求参数到redis
        redisUtil.lSet(JobConts.PARAM_KEY, paramBean);
        //保存一下结果到redis
        redisUtil.lSet(JobConts.RESULT_KEY, bean);
        //响应的内容
        return new ResponseEntity<Object>(bean, httpHeaders, HttpStatus.OK);
    }
}
